package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.LoadingDialog;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/25.
 * 添加提现账户
 */

public class AddAlipayAccountActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.alipay_withdraw_et_name)
    EditText alipay_withdraw_et_name;
    @BindView(R.id.alipay_withdraw_et_account)
    EditText alipay_withdraw_et_account;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("添加提现账号");
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_add_alipay_account;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    @OnClick(R.id.alipay_withdraw_tv_money_commit)
    public void onClickAddLLAccount() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(alipay_withdraw_et_name.getText().toString().trim())) {
            ToastUitl.showShort("支付宝姓名不能为空");
            return;
        } else if (TextUtils.isEmpty(alipay_withdraw_et_account.getText().toString())) {
            ToastUitl.showShort("支付宝账号不能为空");
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .postUrlRequestAddAliAccount(UserUtils.getUserToken(),
                        alipay_withdraw_et_account.getText().toString(),
                        alipay_withdraw_et_name.getText().toString().trim(), new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
                                LoadingDialog.showDialogForLoading(AddAlipayAccountActivity.this);
                            }

                            @Override
                            public void onCompleted() {
                              LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
                                LoadingDialog.cancelDialogForLoading();
                                try {
                                    Gson gson = new Gson();
                                    BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                                    if (baseBean.getError_code() == 0) {
                                        ToastUitl.showShort("添加账号成功");
                                        finish();
                                    } else if (baseBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(baseBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });


    }


}
